package com.hexacta.hrs.domain.staffing.relationship;

import java.util.Calendar;

import org.vectrics.employee.Employee;
import org.vectrics.recruiter.job.JobFunction;

import com.hexacta.hrs.domain.staffing.ParticionablePorFecha;
import com.hexacta.hrs.domain.staffing.Project;
import com.hexacta.hrs.domain.staffing.Requirement;
import com.hexacta.hrs.domain.staffing.assignment.EmployeeAssignment;

/**
 * Representa a la relaci�n que existe entre las asignaciones y los
 * requerimientos de un proyecto.
 * 
 * @author ncortinez
 * 
 */
public class RequirementAssignmentRelationship implements Cloneable, ParticionablePorFecha {

    private Long requirementId = null;
    
    private Requirement requirement = null;

    private Project project = null;

    private Employee employee = null;

    private JobFunction role = null;

    private double porcentage = 0;

    private Calendar startDate = null;

    private Calendar endDate = null;

    private String comments = null;

    private boolean tentative = false;

    public static RequirementAssignmentRelationship createRequirementAssignment(final EmployeeAssignment assignment,
            final Requirement requirement) {
        RequirementAssignmentRelationship relationship = new RequirementAssignmentRelationship();
        relationship.setRequirement(requirement);
        relationship.setProject(assignment.getProject());
        relationship.setEmployee(assignment.getEmployee());
        relationship.setRole(assignment.getAssignedRole());
        relationship.setStartDate(assignment.getStartDate());
        relationship.setEndDate(assignment.getEndDate());
        relationship.setTentative(false);
        relationship.setComments(assignment.getComments());

        // El porcentaje cubierto es el menor entre el requerimiento y la
        // asignacion:
        if (assignment.getAssignmentPorcentage() <= requirement.getAssignmentPorcentage()) {
            relationship.setPorcentage(assignment.getAssignmentPorcentage());
        } else {
            relationship.setPorcentage(requirement.getAssignmentPorcentage());
        }

        return relationship;
    }

    public static RequirementAssignmentRelationship createNonRequirementAssignment(final EmployeeAssignment assignment) {
        RequirementAssignmentRelationship relationship = new RequirementAssignmentRelationship();
        relationship.setProject(assignment.getProject());
        relationship.setEmployee(assignment.getEmployee());
        relationship.setRole(assignment.getAssignedRole());
        relationship.setPorcentage(assignment.getAssignmentPorcentage());
        relationship.setStartDate(assignment.getStartDate());
        relationship.setEndDate(assignment.getEndDate());
        relationship.setTentative(false);
        relationship.setComments(assignment.getComments());
        return relationship;
    }

    public static RequirementAssignmentRelationship createUnassignedRequirement(final Requirement requirement) {
        RequirementAssignmentRelationship relationship = new RequirementAssignmentRelationship();

        // TODO: relationship.setProject(requirement.getProject());
        relationship.setRequirement(requirement);
        relationship.setRole(requirement.getRequiredRol());
        relationship.setPorcentage(requirement.getAssignmentPorcentage());
        relationship.setStartDate(requirement.getStartDate());
        relationship.setEndDate(requirement.getEndDate());
        relationship.setTentative(false);
        relationship.setRequirementId(requirement.getId());
        // TODO: no existen comentatario, no?
        // relationship.setComments(comments);

        return relationship;
    }

    public RequirementAssignmentRelationship(final Employee employee, final JobFunction role,
            final double assignmentPorcentage, final Calendar startDate, final Calendar endDate) {
        super();
        this.employee = employee;
        this.role = role;
        porcentage = assignmentPorcentage;
        this.startDate = startDate;
        this.endDate = endDate;
    }

    public RequirementAssignmentRelationship() {
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public int customHashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + (employee == null ? 0 : employee.hashCode());
        result = prime * result + (role == null ? 0 : role.hashCode());
        result = prime * result + (tentative ? 1231 : 1237);
        return result;
    }

    public boolean customEquals(final Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        RequirementAssignmentRelationship other = (RequirementAssignmentRelationship) obj;
        if (employee == null) {
            if (other.employee != null)
                return false;
        } else if (!employee.equals(other.employee))
            return false;
        if (role == null) {
            if (other.role != null)
                return false;
        } else if (other.role == null)
            return false;
        else if (!role.getDescription().equals(other.role.getDescription()))
            return false;
        if (tentative != other.tentative)
            return false;
        return true;
    }

    /**
     * GETTERS and SETTERS
     */
    
    public Project getProject() {
        return project;
    }

    public Requirement getRequirement() {
		return requirement;
	}

	public void setRequirement(Requirement requirement) {
		this.requirement = requirement;
	}

	public void setProject(final Project project) {
        this.project = project;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(final Employee employee) {
        this.employee = employee;
    }

    public JobFunction getRole() {
        return role;
    }

    public void setRole(final JobFunction role) {
        this.role = role;
    }

    public double getPorcentage() {
        return porcentage;
    }

    public void setPorcentage(final double assignmentPorcentage) {
        porcentage = assignmentPorcentage;
    }

    public Calendar getStartDate() {
        return startDate;
    }

    public void setStartDate(final Calendar startDate) {
        this.startDate = startDate;
    }

    public Calendar getEndDate() {
        return endDate;
    }

    public void setEndDate(final Calendar endDate) {
        this.endDate = endDate;
    }

    public String getComments() {
        return comments;
    }

    public void setComments(final String comments) {
        this.comments = comments;
    }

    public boolean isTentative() {
        return tentative;
    }

    public void setTentative(final boolean tentative) {
        this.tentative = tentative;
    }

    /**
     * @return the requirementId
     */
    public Long getRequirementId() {
        return requirementId;
    }

    /**
     * @param requirementId
     *            the requirementId to set
     */
    public void setRequirementId(final Long requirementId) {
        this.requirementId = requirementId;
    }
}
